package com.zl.gmadmin.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zl.gmadmin.common.Status;
import com.zl.gmadmin.exception.SecurityException;
import com.zl.gmadmin.service.auth.CustomUserDetailsService;
import com.zl.gmadmin.vo.user.UserPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/2/7 13:58
 */
@Component
public class SecurityUtils {

    private final UserDetailsService userDetailsService;

    public SecurityUtils(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    /**
     * 获取当前登录的用户
     * @return UserDetails
     */
    public static UserDetails getCurrentUser() {
        CustomUserDetailsService userDetailsService = SpringContextHolder.getBean(CustomUserDetailsService.class);
        return userDetailsService.loadUserByUsername(getCurrentUsername());
    }

    /**
     * 获取系统用户名称
     *
     * @return 系统用户名称
     */
    public static String getCurrentUsername() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new SecurityException(Status.UNAUTHORIZED);
        }
        if (authentication.getPrincipal() instanceof UserPrincipal) {
            UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
            return userPrincipal.getUsername();
        }
        throw new SecurityException(Status.NOT_FIND_LOGIN_INFO);
    }

    /**
     * 获取系统用户ID
     * @return 系统用户ID
     */
    public static Long getCurrentUserId() {
        UserPrincipal userDetails = (UserPrincipal) getCurrentUser();
        return userDetails.getId();
    }

    /**
     * 获取当前用户的数据权限
     * @return /
     */
    public static List<Long> getCurrentUserDataScope(){
        UserDetails userDetails = getCurrentUser();
        JSONArray array = cn.hutool.json.JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
        return JSONUtil.toList(array,Long.class);
    }

    /**
     * 获取数据权限级别
     * @return 级别
     */
//    public static String getDataScopeType() {
//        List<Long> dataScopes = getCurrentUserDataScope();
//        if(dataScopes.size() != 0){
//            return "";
//        }
//        return DataScopeEnum.ALL.getValue();
//    }



//    public  void logInAs(String username) {
//
//        UserDetails user = userDetailsService.loadUserByUsername(username);
//        if (user == null) {
//            throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
//        }
//
//        SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() {
//            @Override
//            public Collection<? extends GrantedAuthority> getAuthorities() {
//                return user.getAuthorities();
//            }
//
//            @Override
//            public Object getCredentials() {
//                return user.getPassword();
//            }
//
//            @Override
//            public Object getDetails() {
//                return user;
//            }
//
//            @Override
//            public Object getPrincipal() {
//                return user;
//            }
//
//            @Override
//            public boolean isAuthenticated() {
//                return true;
//            }
//
//            @Override
//            public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
//
//            }
//
//            @Override
//            public String getName() {
//                return user.getUsername();
//            }
//        }));
//        org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
//    }
}
